﻿using Atlanchim.Crm.Mui.Commands;
using Atlanchim.Crm.Mui.Helpers;
using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace Atlanchim.Crm.Mui.Pages
{
    public abstract class AbstractPageViewModel : INotifyPropertyChanged, IDisposable
    {
        /// <summary>
        /// Iniformation affichée en complément du type d'élément
        /// </summary>
        public string Complement { get; set; }

        /// <summary>
        /// Information complémentaire affichée lors de la suppression d'un élément
        /// </summary>
        public string DeleteWarning { get; set; }

        private LinkCollection _tabLinks = new LinkCollection();
        /// <summary>
        /// Collection des liens additionnels (Par exemple - Contacts et Actions)
        /// </summary>
        public LinkCollection TabLinks
        {
            get { return _tabLinks; }
            set
            {
                _tabLinks = value;
                OnPropertyChanged("TabLinks");
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        #region Chargement

        private string _fragment = string.Empty;
        /// <summary>
        /// Paramètre passé à la page via l'url
        /// </summary>
        public string Fragment { 
            get {return this._fragment;}
            set
            {
                this._fragment = value;

                if (!string.IsNullOrEmpty(this._fragment))
                {
                    this.DicFragments = FragmentHelper.Split(this._fragment);
                }
            }
        }

        public Dictionary<string, string> DicFragments { get; set; }

        /// <summary>
        /// Url d'accès à la page d'édition
        /// </summary>
        public abstract string EditUrl { get; set; }

        /// <summary>
        /// Méthode de chargement à surcharger
        /// </summary>
        public abstract void Load();

        #endregion Chargement
        //---------------------------------------------------------------------------------------------------------------------------------------

        //---------------------------------------------------------------------------------------------------------------------------------------
        #region suppression

        /// <summary>
        /// Demande de suppression de l'entité courante
        /// </summary>
        public void DeleteEntity()
        {
            string message = "Etes-vous certain de vouloir supprimer l'élément en cours ?";
            if (!string.IsNullOrEmpty(DeleteWarning))
            {
                message += Environment.NewLine + Environment.NewLine + DeleteWarning;
            }

            MessageBoxResult answer = ModernDialog.ShowMessage(message, "Confirmation de suppression", System.Windows.MessageBoxButton.YesNo);
            if (answer == MessageBoxResult.Yes)
            {
                OnDeleteCurrentEntity();

                TopNavigationHelper.NavigateBackTopFrame();
            }
        }

        /// <summary>
        /// Traitement de la suppression de l'entité
        /// </summary>
        protected abstract void OnDeleteCurrentEntity();
        #endregion suppression
        //---------------------------------------------------------------------------------------------------------------------------------------


        //---------------------------------------------------------------------------------------------------------------------------------------
        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion INotifyPropertyChanged
        //---------------------------------------------------------------------------------------------------------------------------------------

        public void Dispose()
        {
            //this._editCommand = null;
        }
    }
}
